 package com.zu.commu.auto1_0.protocol.waterSiteWarning;

 import com.zu.commu.auto1_0.DataType1_0;
   import com.zu.commu.auto1_0.protocol.CheckData;
   import com.zu.commu.auto1_0.protocol.Util;
   import com.zu.commu.exception.CommuException;

 public class WaterSiteWarningProtocol
 {
   public byte[] remoteData(String meterNo, String value)
     throws Exception
   {
     String[] temp = (String[])null;
     try {
       value = checkValue(value);
       temp = canonValue(value);
     } catch (Exception e) {
       throw e;
     }
     byte[] buf = new byte[17];
     buf[0] = 104;
     buf[1] = 11;
     buf[2] = 11;
     buf[3] = 104;
     Util u = new Util();
     try {
       byte[] b = u.hex2Byte(meterNo);
       for (int i = 0; i < b.length; i++)
         buf[(4 + i)] = b[i];
     }
     catch (Exception e) {
       throw new CommuException().setMessage(
         "测控器地址号不合法！" + e.getMessage(), null);
     }
     buf[10] = 64;
     buf[11] = (byte)DataType1_0.WATERSITEWARNING.toInt();
     try
     {
       byte[] b = u.hex2Byte(temp[0]);
       for (int i = 0; i < b.length; i++)
         buf[(12 + i)] = b[i];
     }
     catch (Exception e) {
       throw new CommuException().setMessage("水位报警下限数据不合法值！" + e.getMessage(),
         e);
     }
     try {
       byte[] b = u.hex2Byte(temp[1]);
       buf[14] = b[0];
     } catch (Exception e) {
       throw new CommuException().setMessage("水位报警下限数据不合法值！" + e.getMessage(),
         e);
     }

     buf[15] = new CheckData().getCrcCheck(buf, 4, 14);
     buf[16] = 22;
     return buf;
   }

   public String checkValue(String value)
     throws CommuException
   {
     value = new Util().cleanDotNumber(value);
     if (value.equals("")) {
       throw new CommuException().setMessage("水位报警下限数据不合法值！", null);
     }
     if (value.startsWith("-")) {
       throw new CommuException().setMessage("水位报警下限数据不合法值！", null);
     }
     int dot = value.indexOf('.');
     if (dot == 0) {
       throw new CommuException().setMessage("水位报警下限数据不合法值！", null);
     }
     if ((dot > 0) &&
       (value.length() - dot > 3)) {
       value = value.substring(0, dot + 3);
     }

     double temp = 0.0D;
     try {
       temp = Double.parseDouble(value);
     } catch (Exception e) {
       throw new CommuException().setMessage("水位报警下限数据不合法值！", null);
     }
     if (temp > 9999.0D) {
       throw new CommuException().setMessage("水位报警下限数据超出合法值范围！", null);
     }
     return temp+"";
   }

   public String[] canonValue(String temp)
   {
     int dot = temp.indexOf('.');
     String[] rr = new String[2];
     if (dot < 0) {
       rr[0] = temp;
       rr[1] = "00";
     } else {
       rr[0] = temp.substring(0, dot);
       rr[1] = temp.substring(dot + 1);
     }

     if (rr[0].length() < 4) {
       int n = 4 - rr[0].length();
       for (int i = 0; i < n; i++) {
         rr[0] = ("0" + rr[0]);
       }
     }
     if (rr[1].length() < 2) {
       int n = 2 - rr[1].length();
       for (int i = 0; i < n; i++) {
         rr[1] = ("0" + rr[1]);
       }
     }

     return rr;
   }
 }
